﻿// See https://aka.ms/new-console-template for more information
using System;
using OpenCvSharp;
using WampShare;
using WampSharp.V2;
using WampSharp.V2.Client;

Console.WriteLine("Hello, client!");


await Task.Delay(2000);
//const string location = "ws://127.0.0.1:8080/ws";

DefaultWampChannelFactory channelFactory = new DefaultWampChannelFactory();

IWampChannel channel = channelFactory.CreateJsonChannel(ICamService.Location, ICamService.RealmStr);

IWampRealmProxy realmProxy = channel.RealmProxy;



await channel.Open().ConfigureAwait(false);

ICamService proxy =
    channel.RealmProxy.Services.GetCalleeProxy<ICamService>();


//Subject
int received = 0;
IDisposable subscription = null;

subscription =
    realmProxy.Services.GetSubject<byte[]>(ICamService.OpenCamTopic)
              .Subscribe(x =>
              {
                  Console.WriteLine($"Got Event: {x.Length}");

                  received++;

                  if (received > 50)
                  {
                      Console.WriteLine("Closing ..");
                      subscription?.Dispose();
                  }
              },
              ex => { Console.WriteLine($"发送出错：{ex}"); } 
              );

// This line is required in order to release the WebSocket thread, otherwise it will be blocked by the Console.ReadLine() line.
await Task.Yield();


//test
string result = await proxy.OpenCam();
Console.WriteLine(result);

await Task.Delay(6000);
result = await proxy.CloseCam();
Console.WriteLine(result);

Console.ReadLine();
